package com.ygqh.baby.handler;

import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.util.StringUtil;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.PromotionStatus;
import com.ygqh.baby.model.ActivitySingleGift;
import com.ygqh.baby.po.YgChannel;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.YgChannelService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 〈单品赠〉
 * 买a送b
 *
 * @author guohao
 * @date 2019/4/16 15:54
 * @since 1.0.0
 */
@Component
public class SingleGiftHandler {

    @Autowired
    private YgChannelService ygChannelService;
    @Autowired
    private RedisDao redisDao;
    private static final String SINGLE_GIFT_KEY_PREFIX = "SingleGiftActivity_%s";

    public Message saveSingleGift(ActivitySingleGift singleGift) {

        if (singleGift.isChannel()) {
            YgChannel channel = ygChannelService.findByCode(singleGift.getSourceCode());
            if (channel == null) {
                return Message.error("source值不存在", singleGift.getSourceCode());
            }
        }

        addSingleGiftToRedis(singleGift);

        return Message.success(1);
    }

    private void addSingleGiftToRedis(ActivitySingleGift gift) {
        String redisKey = getRedisKey(gift.isChannel());
//        Date date = DateConvertUtils.addDay(gift.getEndDate(), 90);
        redisDao.hset(redisKey, gift.getSourceCode(), JSON.toJSONString(gift));
    }

    public ActivitySingleGift getActivitySingleGift(String sourceCode, boolean isChannel) {
        String res = redisDao.hget(getRedisKey(isChannel), sourceCode);
        return JSON.parseObject(res, ActivitySingleGift.class);
    }

    public List<ActivitySingleGift> getUseAbleList(String sourceCode) {
        List<ActivitySingleGift> activitySingleGiftList = getActivitySingleGiftList(false);
        if (StringUtil.isNotBlank(sourceCode)) {
            ActivitySingleGift activitySingleGift = getActivitySingleGift(sourceCode, true);
            if (activitySingleGift != null) {
                activitySingleGiftList.add(activitySingleGift);
            }
        }
        return activitySingleGiftList.stream().filter(gift -> gift.getPromotionStatus().equals(PromotionStatus.Running)).collect(Collectors.toList());
    }


    public List<ActivitySingleGift> getActivitySingleGiftList(Boolean isChannel) {
        if (isChannel == null) {
            List<ActivitySingleGift> activitySingleGiftList = doGetActivitySingleGiftList(true);
            activitySingleGiftList.addAll(doGetActivitySingleGiftList(false));
            return activitySingleGiftList;
        }
        return doGetActivitySingleGiftList(isChannel);
    }

    public List<ActivitySingleGift> doGetActivitySingleGiftList(boolean isChannel) {
        List<String> res = redisDao.hget(getRedisKey(isChannel));
        List<ActivitySingleGift> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(res)) {
            for (String str : res) {
                ActivitySingleGift activitySingleGift = JSON.parseObject(str, ActivitySingleGift.class);
                list.add(activitySingleGift);
            }
        }
        return list;
    }


    private String getRedisKey(boolean channel) {
        return String.format(SINGLE_GIFT_KEY_PREFIX, channel);
    }

    public Message deleteSingleGift(Boolean isChannel, String sourceCode) {
        Assert.notNull(isChannel, "isChannel can't be null.");
        Assert.notNull(sourceCode, "sourceCode can't be null.");
        String redisKey = getRedisKey(isChannel);
        Long hdel = redisDao.hdel(redisKey, sourceCode);
        return Message.success(hdel);
    }
}
